<template>
    <div class="my-container summaryAnalysisPage">
        <el-row :gutter="20">
            <el-col class="aaa" :span="show ? rowObj.T1 : rowObj.F1" :xs="show ? rowObj.T2 : rowObj.F2">
                <workSiteLeft ref="workSiteLeft" :show="show" @showLeft="showLeft" @queryVal="queryVal" />
            </el-col>
            <el-col :span="show ? rowObj.T3 : rowObj.F3" :xs="show ? rowObj.T4 : rowObj.F4">

                <div class="container-body">
                    <el-form :inline="true" :model="queryParams" size="small">
                        <el-button style="margin: 3px 8px 0 0;" size="mini"
                            :icon="show ? 'el-icon-s-fold' : 'el-icon-s-unfold'" circle @click="showLeft(show)"></el-button>
                        <el-form-item label="拌合楼">
                            <el-select style="width: 160px" v-model="queryParams.stationCode" clearable size="mini"
                                placeholder="请选择拌合楼">
                                <el-option v-for="item in cementListData" :key="item.id" :label="item.name"
                                    :value="item.code">
                                </el-option>
                            </el-select>
                            <!-- <zhjd-cement-station v-model="queryParams.stationCode" clearable /> -->
                        </el-form-item>
                        <el-form-item label="选择时间">
                            <el-select filterable v-model="timeType" style="width: 100px; margin-right: 8px"
                                :popper-append-to-body="false" @change="handleTimeType">
                                <el-option label="投产以来" value="all"></el-option>
                                <el-option label="按年" value="year"></el-option>
                                <el-option label="自定义" value="other"></el-option>
                            </el-select>
                            <deatetime v-if="'other' == timeType" ref="deatetime" v-model="queryParams.timeRange">
                            </deatetime>
                            <el-date-picker style="width: 120px" v-if="'year' == timeType" v-model="queryParams.year"
                                value-format="yyyy" format="yyyy" type="year" placeholder="选择年">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item>
                            <el-button icon="el-icon-search" type="primary" @click="handleQuery">查询</el-button>
                        </el-form-item>
                    </el-form>

                    <el-table :data="dataList" v-loading="loading" border size="mini" highlight-current-row stripe>
                        <el-table-column prop="stationName" label="拌合楼" align="center"></el-table-column>
                        <el-table-column prop="matchCode" label="配方编号" align="center"></el-table-column>
                        <el-table-column prop="projectName" label="项目名称" align="center"></el-table-column>
                        <el-table-column prop="pavingLevelLabel" label="摊铺层级" align="center"></el-table-column>
                        <el-table-column prop="number" label="已生产批次" align="center"></el-table-column>
                        <el-table-column prop="alarmNumber" label="不合格批次" align="center"></el-table-column>
                        <el-table-column prop="weight" label="已生产重量(吨)" align="center"></el-table-column>
                        <el-table-column prop="alarmWeight" label="不合格重量(吨)" align="center"></el-table-column>
                    </el-table>
                    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                        :limit.sync="queryParams.pageSize" @pagination="getList" />
                </div>

            </el-col>
        </el-row>

    </div>
</template>
  
<script>
import workSiteLeft from "@/views/components/workSiteLeft";

import {
    cementList,
    getLastData,
    pageQualityStatistics
} from "@/api/waterStability/productionQuality/summaryAnalysis";
export default {
    name: 'summaryAnalysis',
    components: {
        workSiteLeft
    },
    data() {
        return {
            show: true,
            rowObj: {
                T1: 3,
                T2: 24,
                T3: 21,
                T4: 24,
                F1: 0,
                F2: 0,
                F3: 24,
                F4: 24,
            },
            dataList: [],
            loading: false,
            cementListData: [],
            timeType: 'all',
            total: 0,
            matchCodeList: [],
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                stationCode: undefined,
                year: undefined,
                timeRange: [undefined, undefined]
            },
        };
    },
    filters: {
        showNumber(value, unit) {
            if (null == value || undefined == value) {
                return '';
            }
            return value + ' ' + unit;
        },
        showEmpty(value) {
            if (null == value || undefined == value) {
                return '/';
            }
            return value;
        }
    },
    mounted() {
        this.init();
        this.getCementList();
    },
    methods: {
        showLeft(show) {
            this.show = !show;
        },
        queryVal(val) {
            this.queryParams.id = val.id;
            this.handleQuery();
        },
        // 查询拌合楼下拉列表
        getCementList() {
            cementList().then((res) => {
                this.cementListData = res.data || [];
            });
        },
        async init() {
            await this.getLastData();
            // this.getList();
        },
        /**
         * 获取最新数据
         */
        getLastData() {
            getLastData().then((res) => {
                if (!!res.data) {
                    let dischargeTime = res.data.dischargeTime;
                    if (4 < dischargeTime.length) {
                        this.queryParams.year = dischargeTime.substring(0, 4);
                        this.timeType = 'year';
                    }
                    this.queryParams.stationCode = res.data.stationCode;
                }
                this.getList();
            });
        },

        getList() {
            this.loading = true;
            console.log(this.queryParams);
            setTimeout(() => {
                pageQualityStatistics(this.queryParams).then((response) => {
                    this.dataList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            }, 0);

        },

        /**
         * 获取查询条件
         */
        getSearchParams() {
            let params = {};
            if (!!this.queryParams.stationCode) {
                params['queryParams'] = this.queryParams.stationCode;
            }
            if ('year' == this.timeType) {
                if (!!this.queryParams.year) {
                    params['year'] = this.queryParams.year;
                }
            } else if ('other' == this.timeType) {
                if (!!this.queryParams.timeRange && 2 == this.queryParams.timeRange.length) {
                    params['startDate'] = this.queryParams.timeRange[0];
                    params['endDate'] = this.queryParams.timeRange[1];
                }
            }

            return params;
        },
        handleTimeType(val) {
            if (val == "year") {
                this.queryParams["year"] = this.lastYear;
            }
        },
        handleQuery() {
            this.queryParams = this.getSearchParams();
            this.queryParams.pageNum = 1;
            this.getList();
        },
        getSearchParams() {
            const params = {};
            for (const key in this.queryParams) {
                if (key === "time") {
                    const time = this.queryParams[key];
                    if (time && time.length === 2) {
                        params.startTime = time[0];
                        params.endTime = time[1];
                    }
                } else {
                    params[key] = this.queryParams[key];
                }
            }
            return params;
        },
    }
};
</script>
  
<style lang="scss" scoped>
.summaryAnalysisPage {
    padding: 16px;
}
</style>
  